package com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku_zhijian.R;
import com.xiaoxing.zhinenghuacangku_zhijian.R2;
import com.xiaoxing.zhinenghuacangku_zhijian.di.component.DaggerJianCeYuanJianCeComponent;
import com.xiaoxing.zhinenghuacangku_zhijian.di.module.JianCeYuanJianCeModule;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.contract.JianCeYuanJianCeContract;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.presenter.JianCeYuanJianCePresenter;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.entity.JianCeYuanJianCe;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_ZHI_JIAN_ACTIVITY_JIAN_CE_YUAN_JIAN_CE)
public class JianCeYuanJianCeActivity extends BaseActivity<JianCeYuanJianCePresenter> implements JianCeYuanJianCeContract.View {


    @BindView(R2.id.tv_sao_miao_nei_rong)
    TextView tvSaoMiaoNeiRong;
    @BindView(R2.id.tv_feng_qian_qu_ming_cheng)
    TextView tvFengQianQuMingCheng;
    private String scan_result;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerJianCeYuanJianCeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .jianCeYuanJianCeModule(new JianCeYuanJianCeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_jian_ce_yuan_jian_ce; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_jian_ce_yuan_jian_ce));
        //getJianCeYuanJianCeData();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 102) {//扫描物料二维码(有二维码的质检样品)
            Bundle extras = data.getExtras();
            scan_result = extras.getString("scan_result");
            tvSaoMiaoNeiRong.setText("已扫描的封签码：" + scan_result);
            getQualityItemTestZone();
        }
    }

    /**
     * @param
     * @return
     * @method
     * @description 扫描封签码查询封样区的名称
     * @date: 2020/9/4 0004 15:50
     * @author: 小星
     */
    private void getQualityItemTestZone() {
        showLoading();

        String methodNames = "GetQualityItemTestZone";
        SoapParams paramss = null;
        paramss = new SoapParams();
        paramss.put("simpleCode", scan_result);
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();
                try {
                    String result = object.getProperty(methodNames + "Result").toString();
                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100") && !result.equals("false")) {
                        tvFengQianQuMingCheng.setText("封样区的名称:" + result);
                    } else {
                        showMessage("获取封签区信息失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void getJianCeYuanJianCeDataSuccess(JianCeYuanJianCe entityData) {


    }

    private void getJianCeYuanJianCeData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getJianCeYuanJianCeData(map);
    }

    @OnClick(R2.id.btn_fa_song_zhi_jian_ren_wu)
    public void onViewClicked() {
        if (TextUtils.isEmpty(scan_result)) {
            showMessage("请先扫描封签码");
            return;
        }
        setSimpleReceiveSuccess();
    }

    /**
     * @param
     * @return
     * @method
     * @description 发送质检任务
     * @date: 2020/9/4 0004 15:52
     * @author: 小星
     */
    private void setSimpleReceiveSuccess() {
        showLoading();

        String methodNames = "setSimpleReceiveSuccess";
        SoapParams paramss = null;
        paramss = new SoapParams();
        paramss.put("simpleCode", scan_result);
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();
                try {
                    String result = object.getProperty(methodNames + "Result").toString();
                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100") && !result.equals("false")) {
                        showMessage("发送质检任务成功");
                        scan_result = "";
                        tvSaoMiaoNeiRong.setText("已扫描的封签码:");
                        tvFengQianQuMingCheng.setText("封样区的名称:");

                    } else {
                        showMessage("发送质检任务失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    @OnClick(R2.id.img_sao_miao)
    public void imgSaoMiao() {

        Intent newIntent = new Intent(this, ScanZhiJianActivity.class);
        mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, 102);
        // 开始一个新的 Activity等候返回结果
        startActivityForResult(newIntent, 102);
    }


}
